package com.ruoyi.project.erp.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.project.erp.dto.CompleteTaskDto;
import com.ruoyi.project.erp.dto.CreateProjectDto;
import com.ruoyi.project.erp.dto.CreateTaskDTO;
import com.ruoyi.project.erp.dto.UpdateTaskDTO;
import com.ruoyi.project.erp.vo.TaskInfoVo;
import com.ruoyi.project.erp.vo.TaskVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.erp.domain.Task;
import com.ruoyi.project.erp.service.ITaskService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 任务Controller
 * 
 * @author ruoyi
 * @date 2025-06-19
 */
@RestController
@RequestMapping("/erp/task")
public class TaskController extends BaseController
{
    @Autowired
    private ITaskService taskService;

    /**
     * 查询任务列表
     */
    @PreAuthorize("@ss.hasPermi('erp:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(Task task)
    {
        startPage();
        List<Task> list = taskService.selectTaskList(task);
        return getDataTable(list);
    }

    /**
     * 查询任务列表
     */
    @PreAuthorize("@ss.hasPermi('erp:task:list')")
    @PostMapping("/completeTask")
    public AjaxResult completeTask(@RequestBody CompleteTaskDto dto)
    {
        return success(taskService.completeTask(dto));
    }
    /**
     * 查询任务列表
     */
    @PreAuthorize("@ss.hasPermi('erp:task:list')")
    @GetMapping("/currentTask")
    public TableDataInfo currentTask(Date startTime)
    {
        startPage();
        List<TaskVo> list = taskService.currentTask(startTime);
        return getDataTable(list);
    }
    /**
     * 查询任务详情
     */
    @GetMapping("/queryTaskInfo")
    public AjaxResult queryTaskInfo(Long taskId)
    {
        TaskInfoVo info = taskService.queryTaskInfo(taskId);
        return success(info);
    }

    /**
     * 导出任务列表
     */
    @PreAuthorize("@ss.hasPermi('erp:task:export')")
    @Log(title = "任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Task task)
    {
        List<Task> list = taskService.selectTaskList(task);
        ExcelUtil<Task> util = new ExcelUtil<Task>(Task.class);
        util.exportExcel(response, list, "任务数据");
    }

    /**
     * 获取任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('erp:task:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(taskService.selectTaskDetailById(id));
    }

    /**
     * 新增任务
     */
    @PreAuthorize("@ss.hasPermi('erp:task:add')")
    @Log(title = "任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Task task)
    {
        return toAjax(taskService.insertTask(task));
    }

    /**
     * 修改任务
     */
    @PreAuthorize("@ss.hasPermi('erp:task:edit')")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Task task)
    {
        return toAjax(taskService.updateTask(task));
    }

    /**
     * 删除任务
     */
    @PreAuthorize("@ss.hasPermi('erp:task:remove')")
    @Log(title = "任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(taskService.deleteTaskByIds(ids));
    }







    @Log(title = "新建任务", businessType = BusinessType.INSERT)
    @PostMapping("/createTask")
    public AjaxResult createTask(@RequestBody CreateTaskDTO dto)
    {
        taskService.createTask(dto);
        return AjaxResult.success();
    }

    @Log(title = "编辑任务", businessType = BusinessType.UPDATE)
    @PostMapping("/updateTask")
    public AjaxResult updateTask(@RequestBody UpdateTaskDTO dto)
    {
        taskService.appUpdateTask(dto);
        return AjaxResult.success();
    }


    @Log(title = "批量添加通用任务", businessType = BusinessType.INSERT)
    @PostMapping("/batchAddToAllProjects")
    public AjaxResult batchAddToAllProjects(@RequestBody Task task) {
        // 参数校验：确保任务名称等必要信息不为空
        if (task.getTaskName() == null || task.getTaskName().trim().isEmpty()) {
            return AjaxResult.error("任务名称不能为空");
        }

        int addedCount = taskService.batchAddTaskToAllProjects(task);
        return AjaxResult.success("操作成功，已为 " + addedCount + " 个项目添加了任务");
    }
}
